package edu.gatech.arktos.testcases;

import java.io.File;

import javax.swing.JTextArea;

import org.apache.commons.io.FileUtils;

import edu.gatech.arktos.Constants;
import edu.gatech.arktos.GradesDB;
import edu.gatech.arktos.GradesToolPanel;
import edu.gatech.arktos.Session;
import edu.gatech.arktos.Student;
import junit.framework.TestCase;

public class GradesToolPanelTest extends TestCase {
	private Session session;
	GradesDB db = null;
	GradesToolPanel actualProgramFrame;

	protected void setUp() throws Exception {
		session = new Session();
		session.login(Constants.USERNAME, Constants.PASSWORD);
		db = session.getDBByName(Constants.GRADES_DB);
		actualProgramFrame = new GradesToolPanel();
		super.setUp();
	}

	protected void tearDown() throws Exception {
		session.logout();
		super.tearDown();
	}

	
	  public void testUpdateLabels(){
		  Student curStudent = db.getStudentByName("Ernesta Anderson");
		  String text="Name: ";
		  text += curStudent.getName() + "\n";
		  text += "GTID: ";
		  text += curStudent.getGtid() + "\n";
		  text += "Attendance: ";
		  text += curStudent.getAttendance() +"\n";
		  assertTrue(actualProgramFrame.updateLabels(curStudent).compareTo(text) == 0);
		 
	  }
	 

	  public void testpopulateMenu() 
	  { 
		  
		  String[] names = {"Freddie Catlay","Josepha Jube","Grier Nehling","Shevon Wise","Laraine Smith" ,"Caileigh Raybould","Kym Hiles"
			,"Christine Schaeffer","Genista Parrish","Ernesta Anderson" ,"Wilfrid Eastwood","Rastus Kight","Sheree Gadow","Cynthia Faast"}; 
		  int eq=1, len = names.length, i; 
		  JTextArea jt = new JTextArea();
		  actualProgramFrame.makePullDownMenu(db.getStudents(), jt, db);
		  String[] cmp = actualProgramFrame.returnCombo();
		  for (i=0; i<len; i++)
		  {
			  for(int j = 0; j < names.length; j++)
			  {
				  if(names[j].equals(cmp[i]))
				  {
					  eq = 0;
				  }
			  }
			  assertEquals(0, eq);
		  }
		  
	  
	  }

	public void testsave() {
		Student stud = db.getStudentByName("Ernesta Anderson");
		// make Ernesta Anderson the current student
		actualProgramFrame.updateLabels(stud);
		//Save Ernesta from GUI
		actualProgramFrame.save(stud);
		//open file from GUI
		File file1 = new File(db.getStudentByName("Ernesta Anderson").getName()+".txt");
		// open the file named Ernesta Anderson in the current directory (created by GradesDBTest2 TestFileSave())
		File file2 = new File("ErnestaAnderson.txt");
		// compare the contents of both files
		boolean are_equal = false;
		try {
			are_equal = FileUtils.contentEquals(file1, file2);
		} catch (Exception e) {

		}
		assertTrue(are_equal);
	}
}
